

package com.zk.sqservice.client;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Looper;
import android.util.Log;

import com.zk.sqservice.ISqService;
import com.zk.sqservice.constant.DebugTag;
import com.zk.sqservice.service.SqServiceStub;

import static com.zk.sqservice.constant.DebugTag.TAG;

/**
 * @author zgx
 * //todo 调用服务
 **/
public class SqServiceProxy implements ServiceConnection {


    public static final String SERVICE_DEFAULT_CLASSNAME
            = "com.zk.sqservice.service.SqServiceNative";

    public static final String SERVICE_PACKAGE
            = "com.zk.sqservice.service";

    private static Context gContext;
    public static SqServiceProxy inst;

    private static String gPackageName;
    private static String gClassName;

    /***
     *定时任务
     * **/
    private Worker worker;

    /***
     * 操作服务端的接口
     * **/
    private ISqService service = null;

    private ClientShareMem mClientShareMem;

    private SqServiceProxy() {
        mClientShareMem = new ClientShareMem(gContext);
        //worker = new Worker();
        //worker.start();
    }

    public static void init(Context context, Looper looper, String packageName) {
        if (inst != null) {
            return;
        }

        gContext = context.getApplicationContext();

        gPackageName = (packageName == null ? context.getPackageName() : packageName);
        gClassName = SERVICE_DEFAULT_CLASSNAME;
        Log.i(DebugTag.TAG, "SqServiceProxy ： init gPackageName = " + gPackageName +
                ", gClassName = " + gClassName);

        inst = new SqServiceProxy();
        inst.continueProcessTaskWrappers();
    }

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder binder) {
        Log.d(TAG, "onServiceConnected : remote mars service connected");

        try {
            service = SqServiceStub.Stub.asInterface(binder);
            mClientShareMem.setISqService(service);
            mClientShareMem.dataFlow(123);
        } catch (Exception e) {
            service = null;
            Log.e(TAG, "onServiceConnected err : " + e.toString());
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        service = null;
        Log.d(TAG, "onServiceDisconnected : remote mars service disconnected");
    }

    private void continueProcessTaskWrappers() {

        try {
            if (service == null) {
                Log.d(TAG, String.format("开始绑定服务, packageName: %s, className: %s",
                        gPackageName, gClassName));
                Intent i = new Intent().setClassName(gPackageName, gClassName);
                gContext.startService(i);
                if (!gContext.bindService(i, inst, Service.BIND_AUTO_CREATE)) {
                    Log.e(TAG, "remote mars service bind failed");
                } else {
                    Log.i(TAG, "remote mars service bind ok");
                }
                // Waiting for service connected
                //检查服务是否启动，没有则启动并返回等待下一个50ms再继续；
                return;
            }

            //todo 从队列中获取一个任务，并给他分配一个cmdID,然后调用MarsService的send方法执行真正的发送事件。

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static class Worker extends Thread {

        @Override
        public void run() {

            while (true) {
                inst.continueProcessTaskWrappers();

                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
